How to Add a Header/Footer in Word
Add a header/footer in Word document can make the document easier to navigate and more professional appearance. The information of header/footer varies from one document to another. Despite what some users think, the information can be document title, chapter titles, page number, number of pages, date of creation, creator, date last saved, filename, or pathname. If you do a great job on Word header/footer, no matter you add a simple page number or go fancy by adding custom fields, you can give your readers relevant information about the document through header/footer.
These instructions below are created for C# developers. In this article, we will introduce how to use C# to add a header/footer in Word document. Spire.Doc, a professional .NET Word component, can help us do this job perfectly. With the help of Spire.Doc, we will do the 4 jobs below.
Add a Header/Footer in Word with Text Content
Step 1 Create Project
Create a C# project in visual studio add Spire.Doc.dll as reference. The default setting of Spire.Doc.dll is placed under "C:\Program Files\e-iceblue\Spire.Doc\Bin”. Select assembly Spire.Doc.dll and click OK to add it to the project.
Step 2 Load Word Document
Spire.Doc allows users to create Word document and edit at will. We can also use document.LoadfromFile method to load exist Word document.
//Create word document
Document document = new Document();
Step 3 Add Header/Footer in Word Document
If we create a new Word document via Spire.Doc, we can write in some content into the document. Then, we can design header/footer content at will. Spire.Doc allows users to set position, text style, etc.
private void InsertHeaderFooter(Document document)
{
Section section = document.AddSection();
Paragraph paragraph = section.AddParagraph();
paragraph.AppendText("The sample demonstrates how to insert a header and footer into a document.");
paragraph.ApplyStyle(BuiltinStyle.Heading2);
paragraph = section.AddParagraph();
paragraph.AppendText("Microsoft Word is a word processor designed by Microsoft. It was first released in 1983 under the name Multi-Tool Word for Xenix systems. Subsequent versions were later written for several other platforms including IBM PCs running DOS (1983), the Apple Macintosh (1984), the AT&T Unix PC (1985), Atari ST (1986), SCO UNIX, OS/2, and Microsoft Windows (1989).");
section.PageSetup.DifferentFirstPageHeaderFooter = true;
paragraph = new Paragraph(document);
paragraph.AppendText("Spire.Doc for .NET").CharacterFormat.FontSize = 15;
paragraph.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center;
section.HeadersFooters.FirstPageHeader.Paragraphs.Add(paragraph);
paragraph = new Paragraph(document);
paragraph.AppendText("e-iceblue company Ltd. 2002-2010 All rights reserved").CharacterFormat.FontSize = 15;
paragraph.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center;
section.HeadersFooters.FirstPageFooter.Paragraphs.Add(paragraph);
}
Effective Screenshot:
Header:
These instructions below are created for C# developers. In this article, we will introduce how to use C# to add a header/footer in Word document. Spire.Doc, a professional .NET Word component, can help us do this job perfectly. With the help of Spire.Doc, we will do the 4 jobs below.
- Add a Header/Footer in Word with Text
- Add Page Number into Word Footer
- Add a Header/Footer in Word with Image
- Set Borders for Word Header/Footer
Add a Header/Footer in Word with Text Content
Step 1 Create Project
Create a C# project in visual studio add Spire.Doc.dll as reference. The default setting of Spire.Doc.dll is placed under "C:\Program Files\e-iceblue\Spire.Doc\Bin”. Select assembly Spire.Doc.dll and click OK to add it to the project.
Step 2 Load Word Document
Spire.Doc allows users to create Word document and edit at will. We can also use document.LoadfromFile method to load exist Word document.
//Create word document
Document document = new Document();
Step 3 Add Header/Footer in Word Document
If we create a new Word document via Spire.Doc, we can write in some content into the document. Then, we can design header/footer content at will. Spire.Doc allows users to set position, text style, etc.
private void InsertHeaderFooter(Document document)
{
Section section = document.AddSection();
Paragraph paragraph = section.AddParagraph();
paragraph.AppendText("The sample demonstrates how to insert a header and footer into a document.");
paragraph.ApplyStyle(BuiltinStyle.Heading2);
paragraph = section.AddParagraph();
paragraph.AppendText("Microsoft Word is a word processor designed by Microsoft. It was first released in 1983 under the name Multi-Tool Word for Xenix systems. Subsequent versions were later written for several other platforms including IBM PCs running DOS (1983), the Apple Macintosh (1984), the AT&T Unix PC (1985), Atari ST (1986), SCO UNIX, OS/2, and Microsoft Windows (1989).");
section.PageSetup.DifferentFirstPageHeaderFooter = true;
paragraph = new Paragraph(document);
paragraph.AppendText("Spire.Doc for .NET").CharacterFormat.FontSize = 15;
paragraph.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center;
section.HeadersFooters.FirstPageHeader.Paragraphs.Add(paragraph);
paragraph = new Paragraph(document);
paragraph.AppendText("e-iceblue company Ltd. 2002-2010 All rights reserved").CharacterFormat.FontSize = 15;
paragraph.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center;
section.HeadersFooters.FirstPageFooter.Paragraphs.Add(paragraph);
}
Effective Screenshot:
Header:
Footer:
Add Footer in Word Document with Page Number
Page number usually is placed in the footer of a document. The guide below will show you how to use Spire.Doc add page number in Word footer.
Step 1 Create Project (Same as above)
Step 2 Load Word Document (Same as above)
Step 3 Add Page Number in Word Footer
footerParagraph.AppendField("page number", FieldType.FieldPage);
footerParagraph.AppendText(" of ");
footerParagraph.AppendField("number of pages", FieldType.FieldNumPages);
footerParagraph.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Right;
Add a Header/Footer in Word with Image
Spire.Doc enables users to add image into Word header/footer. The sample below is showing you how to add image into Word header/footer.
HeaderFooter header = section.HeadersFooters.Header;
Paragraph headerParagraph = header.AddParagraph();
DocPicture headerPicture = headerParagraph.AppendPicture(Image.FromFile("Header.png"));
HeaderFooter footer = section.HeadersFooters.Footer;
Paragraph footerParagraph = footer.AddParagraph();
DocPicture footerPicture = footerParagraph.AppendPicture(Image.FromFile("Footer.png"));
Effective Screenshot:
Page number usually is placed in the footer of a document. The guide below will show you how to use Spire.Doc add page number in Word footer.
Step 1 Create Project (Same as above)
Step 2 Load Word Document (Same as above)
Step 3 Add Page Number in Word Footer
footerParagraph.AppendField("page number", FieldType.FieldPage);
footerParagraph.AppendText(" of ");
footerParagraph.AppendField("number of pages", FieldType.FieldNumPages);
footerParagraph.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Right;
Add a Header/Footer in Word with Image
Spire.Doc enables users to add image into Word header/footer. The sample below is showing you how to add image into Word header/footer.
HeaderFooter header = section.HeadersFooters.Header;
Paragraph headerParagraph = header.AddParagraph();
DocPicture headerPicture = headerParagraph.AppendPicture(Image.FromFile("Header.png"));
HeaderFooter footer = section.HeadersFooters.Footer;
Paragraph footerParagraph = footer.AddParagraph();
DocPicture footerPicture = footerParagraph.AppendPicture(Image.FromFile("Footer.png"));
Effective Screenshot:
Set Word Header/Footer Borders
As a powerful .NET/Silverlight component, Spire.Doc enables users to set Word header/footer borders.
//Header border
headerParagraph.Format.Borders.Bottom.BorderType = Spire.Doc.Documents.BorderStyle.Single;
//Space between text and border
headerParagraph.Format.Borders.Bottom.Space = 0.05F;
//Footer border
footerParagraph.Format.Borders.Top.BorderType = Spire.Doc.Documents.BorderStyle.Single;
//Space between text and border
footerParagraph.Format.Borders.Top.Space = 0.05F;
More about Spire.Doc
As a professional and powerful Word component, Spire.Doc doesn’t need Microsoft Office Word Automation but also allows user to directly operate Word document, format and style and insert content to Word document. Spire.Doc which with high quality, powerful functions and cheap price supports Word 97, Word 2003, Word 2007 and Word 2010. Click to learn more...
As a powerful .NET/Silverlight component, Spire.Doc enables users to set Word header/footer borders.
//Header border
headerParagraph.Format.Borders.Bottom.BorderType = Spire.Doc.Documents.BorderStyle.Single;
//Space between text and border
headerParagraph.Format.Borders.Bottom.Space = 0.05F;
//Footer border
footerParagraph.Format.Borders.Top.BorderType = Spire.Doc.Documents.BorderStyle.Single;
//Space between text and border
footerParagraph.Format.Borders.Top.Space = 0.05F;
More about Spire.Doc
As a professional and powerful Word component, Spire.Doc doesn’t need Microsoft Office Word Automation but also allows user to directly operate Word document, format and style and insert content to Word document. Spire.Doc which with high quality, powerful functions and cheap price supports Word 97, Word 2003, Word 2007 and Word 2010. Click to learn more...